﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Frame.Core.Common.Common
{
    public static class EnumHelper
    {
        /// <summary>
        /// 从枚举中获取Description
        /// </summary>
        /// <param name="enumName">需要获取枚举描述的枚举</param>
        /// <returns>描述内容</returns>
        public static string GetDescription(Enum enumName)
        {
            var description = string.Empty;
            var fieldInfo = enumName.GetType().GetField(enumName.ToString());
            var attributes = GetDescriptAttr(fieldInfo);
            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            else
            {
                description = enumName.ToString();
            }
            return description;
        }

        /// <summary>
        /// 获取字段Description
        /// </summary>
        /// <param name="fieldInfo">FieldInfo</param>
        /// <returns>DescriptionAttribute[] </returns>
        private static DescriptionAttribute[] GetDescriptAttr(FieldInfo fieldInfo)
        {
            if (fieldInfo != null)
            {
                return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            }
            return null;
        }

        /// <summary>
        /// 获取枚举所有名称
        /// </summary>
        /// <param name="enumType">枚举类型typeof(T)</param>
        /// <returns>枚举名称列表</returns>
        public static List<string> GetEnumNames(Type enumType)
        {
            return Enum.GetNames(enumType).ToList();
        }

        /// <summary>
        /// 获取所有枚举对应的值
        /// </summary>
        /// <param name="enumType">枚举类型typeof(T)</param>
        /// <returns>枚举值列表</returns>
        public static List<int> GetEnumValues(Type enumType)
        {
            var list = new List<int>();
            foreach (var value in Enum.GetValues(enumType))
            {
                list.Add(Convert.ToInt32(value));
            }
            return list;
        }

        /// <summary>
        /// 获取枚举对应的值
        /// </summary>
        /// <param name="enumType">枚举类型typeof(T)</param>
        /// <returns>枚举值列表</returns>
        public static int GetEnumValue(Type enumType)
        {
            return Convert.ToInt32(enumType);
        }


        /// <summary>
        /// 获取枚举名以及对应的Description
        /// </summary>
        /// <param name="type">枚举类型typeof(T)</param>
        /// <returns>返回Dictionary  ,Key为枚举名，  Value为枚举对应的Description</returns>
        public static Dictionary<object, object> GetNameAndDescriptions(Type type)
        {
            if (type.IsEnum)
            {
                var dic = new Dictionary<object, object>();
                var enumValues = Enum.GetValues(type);
                foreach (Enum value in enumValues)
                {
                    dic.Add(value, GetDescription(value));
                }
                return dic;
            }
            return null;
        }

        //使用
        public enum TestEnum
        {
            [Description("TESTA")] A,
            [Description("TESTB")] B,
            [Description("TESTC")] C
        }

            //获取枚举说明
            //Console.WriteLine("------------------获取枚举说明---------------------");
            //Console.WriteLine(EnumHelper.GetDescription(TestEnum.A));//TESTA

            ////获取枚举所有名称 
            //Console.WriteLine("------------------获取枚举所有名称---------------------");
            //foreach (var item in EnumHelper.GetEnumNames(typeof(TestEnum)))//A B C
            //{
            //    Console.WriteLine(item);
            //}
            ////获取枚举名以及对应的Description
            //Console.WriteLine("------------------获取枚举名以及对应的Description---------------------");
            //foreach (var item in EnumHelper.GetNameAndDescriptions(typeof(TestEnum)))//A:TESTA
            //{
            //    Console.WriteLine(string.Format("Key:  {0},    Value:  {1}  ", item.Key, item.Value));
            //}
            ////获取所有枚举对应的值
            //Console.WriteLine("------------------获取所有枚举对应的值---------------------");//0,1,2
            //foreach (var item in EnumHelper.GetEnumValues(typeof(TestEnum)))
            //{
            //    Console.WriteLine(item);
            //}
    }
}
